<template>
  <div class="order">
    <div class="boxs">
      <!-- <p class="time">
        <span>可提现(元)</span>
        <span class="describe">每周三 09:00 ~ 23:00 可提现</span>
      </p>
      <div class="withdraw">
        <p class="money">{{ form.totalAmount }}</p>
        <van-button class="btn" round type="info" size="small" @click="withdraw">兑换</van-button>
      </div>-->
      <div class="box">
        <div class="fans">
          <p class="trend">每日收益</p>
          <p>
            <van-icon name="chat-o" />
            <span>近30天</span>
          </p>
        </div>
        <div class="echarts" v-if="echartsHide">
          <div id="fans" style="height: 400px"></div>
        </div>
        <div v-else>
          <van-row class="yj one-col">
            <van-col span="24" class="list-title detail-top flex-row space-between">
              <div class="not-image">
                <van-image :src="notImg"></van-image>
                <p class="center mt10">暂无数据</p>
              </div>
            </van-col>
          </van-row>
        </div>
      </div>
      <div class="live">
        <p class="live-title">直播收益</p>
        <div v-if="listHide">
          <div v-if="list&&pageShow">
            <van-list
              class="list"
              v-model="loading"
              :finished="finished"
              :error.sync="error"
              :offset="10"
              finished-text="没有更多了"
              error-text="好像出了点问题，再点下试试？"
              @load="onLoad"
            >
              <div class="row" v-for="(item,index) in list" :key="index">
                <p class="time">{{ item.dateStr }}</p>
                <p class="title van-ellipsis">{{ item.title }}</p>
                <p class="time">+{{ item.goldNum }}</p>
              </div>
            </van-list>
          </div>
        </div>
        <div v-else>
          <van-row class="yj one-col">
            <van-col span="24" class="list-title detail-top flex-row space-between">
              <div class="not-image">
                <van-image :src="notImg"></van-image>
                <p class="center mt10">暂无数据</p>
              </div>
            </van-col>
          </van-row>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
import until from "../../assets/javascript/until";
export default {
  name: "order",
  components: {},
  data() {
    return {
      listHide: true,
      echartsHide: true,
      notImg: require("../../assets/image/notImg.png"),
      form: {},
      until: until,
      time: new Date(),
      list: [],
      start: 0,
      size: 20,
      pageShow: false,
      error: false,
      loading: false,
      isLoading: false,
      finished: false
    };
  },
  created() {
    this.init();
    this.getMoney();
  },
  mounted() {
    this.getCanvas();
  },
  methods: {
    withdraw() {
      if (window.WebViewJavascriptBridge) {
        //安卓
        let that = this;
        this.setupWebViewBridge(
          "submitFromWeb",
          function(responseCallback) {
            // let res = JSON.parse(responseCallback);
            // console.log('res: ',res)
            // for(let i in res){
            //     that.form.imgUrlList.push(res[i])
            // }
          },
          { length: 0 }
        );
      } else {
        let that = this;
        that.$bridge.callHandler(
          "submitFromWeb",
          "伟大的IOS,送你888个媳妇!!!",
          fromNativeData => {
            // 处理返回数据
            alert("fromNativeData");
            // that.operatorId = fromNativeData;
          }
        );
      }
    },
    getMoney() {
      // this.$api
      //   .get(this.$url.app.walletDetail, {})
      //   .then(res => {
      //     if (res) {
      //       this.form = res;
      //     }
      //   })
      //   .catch(error => {});
    },
    onLoad() {
      this.start += 1;
      if (this.start > 1) {
        this.init();
      }
    },
    init() {
      this.$api
        .post(this.$url.data.liveIncome, {
          start: this.start,
          size: this.size
        })
        .then(res => {
          this.loading = false;
          this.list.push(...res.list);
          if (res.list.length < 20) {
            this.finished = true;
          } else {
            this.finished = false;
          }

          if (res.list.length > 0) {
            this.pageShow = true;
            this.listHide = true;
          } else {
            this.listHide = false;
            if (this.start == 1) {
              this.pageShow = false;
            }
          }
        })
        .catch(error => {
          this.error = true;
        });
    },
    getCanvas() {
      this.$api
        .post(this.$url.data.findThirtyDataToLineChat, {})
        .then(res => {
          if (res) {
            this.setCanvas(res);
          }
        })
        .catch(error => {});
    },
    setCanvas(data) {
      if(data.length != 0){
        this.echartsHide = true;
      } else {
        this.echartsHide = false;
      }
      var myChart = this.$echarts.init(document.getElementById("fans"));
      let dateList = data.map(function(item) {
        return item.dateStr;
      });
      let maxNum = 0,
        sortNum = [];
      let valueList = data.map(function(item, index) {
        sortNum.push(item.goldNum);
        return item.goldNum;
      });
      maxNum = sortNum.sort()[sortNum.length - 1];
      // 绘制图表
      myChart.setOption({
        // Make gradient line here
        visualMap: [
          {
            show: false,
            type: "continuous",
            seriesIndex: 0,
            min: 0,
            max: maxNum
          }
        ],
        tooltip: {
          trigger: "axis"
        },
        xAxis: [
          {
            data: dateList
          }
        ],
        yAxis: [
          {
            splitLine: { show: false }
          }
        ],
        grid: [
          {
            bottom: "30%"
          }
        ],
        series: [
          {
            type: "line",
            showSymbol: false,
            data: valueList
          }
        ]
      });
    },
    connectWebViewJavascriptBridge(bridge) {
      bridge.init(function(message, responseCallback) {
        var data = {
          "Javascript Responds": "测试中文!"
        };
        if (responseCallback) {
          responseCallback(data);
        }
      });
      bridge.registerHandler("pageShareMsg", function(data, responseCallback) {
        document.getElementById("show").innerHTML = "data from Java: = " + data;
        if (responseCallback) {
          var responseData = "Javascript Says Right back aka!";
          responseCallback(responseData);
        }
      });
    },
    setupWebViewJavascriptBridge(callback) {
      var u = navigator.userAgent;
      var isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //android终端
      var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
      if (isAndroid) {
        if (window.WebViewJavascriptBridge) {
          callback(WebViewJavascriptBridge);
        } else {
          //添加dom事件
          document.addEventListener(
            "WebViewJavascriptBridgeReady",
            function() {
              callback(WebViewJavascriptBridge);
            },
            false
          );
        }
      }
      if (isiOS) {
        if (window.WebViewJavascriptBridge) {
          return callback(WebViewJavascriptBridge);
        }
        if (window.WVJBCallbacks) {
          return window.WVJBCallbacks.push(callback);
        }
        window.WVJBCallbacks = [callback];
        var WVJBIframe = document.createElement("iframe");
        WVJBIframe.style.display = "none";
        WVJBIframe.src = "https://__bridge_loaded__";
        document.documentElement.appendChild(WVJBIframe);
        setTimeout(function() {
          document.documentElement.removeChild(WVJBIframe);
        }, 0);
      }
    },
    setupWebViewBridge(key, callback, params) {
      let that = this;
      this.setupWebViewJavascriptBridge(function(bridge) {
        var u = navigator.userAgent;
        var isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //android终端
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        //自己定义的全局变量，用来判断当前页面中安卓系统是否进行过一次初始化，页面卸载时取消赋值
        if (!window.hadCalledWindow_WebViewJavascriptBridge) {
          if (isAndroid) {
            window.hadCalledWindow_WebViewJavascriptBridge = true;
            bridge.init(function(message, responseCallback) {
              var data = {
                "Javascript Responds": "测试中文!"
              };

              if (responseCallback) {
                responseCallback(data);
              }
            });
          }
        }
        bridge.callHandler(
          key,
          //安卓系统必须传一个参数，否则捕获不到，因此，默认传入一个_x_变量
          JSON.stringify({
            ...params,
            length: true
          }),
          callback
        );
      });
    }
  }
};
</script>

<style lang="scss" scoped>
@import "@/assets/css/variable.scss";
.order {
  height: 100%;
}
.boxs {
  padding: 10px 15px;
  .time {
    padding: 10px 0;
    color: #2e2e2e;
    span {
      font-size: 0.15rem;
      margin-right: 10px;
      font-weight: bold;
    }
    .describe {
      color: #999;
      font-size: 0.12rem;
      font-weight: normal;
    }
  }
  .withdraw {
    padding: 20px 0 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .money {
      font-size: 0.26rem;
      font-weight: bold;
    }
    .btn {
      width: 80px;
    }
  }
  .box {
    .fans {
      display: flex;
      justify-content: space-between;
      align-items: center;
      p {
        color: #999;
        font-size: 0.12rem;
        span {
          margin-left: 5px;
        }
      }
      .trend {
        color: #2e2e2e;
        font-weight: bold;
        font-size: 0.15rem;
      }
      .day {
        display: flex;
        align-items: center;
      }
    }
  }
  .live {
    margin-top: -60px;
    .live-title {
      font-size: 0.15rem;
      color: #2e2e2e;
      font-weight: bold;
    }
    .list {
      .row {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        .title {
          flex: 1;
          padding: 0 10px 0 5px;
        }
      }
    }
  }
  .echarts {
    margin-top: -30px;
    position: relative;
    z-index: 1;
  }
  .center{
    text-align: center;
  }
}
</style>
